package ninetails;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.SerializerFactory;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.TimeZone;

//import org.apache.log4j.Logger;

public class Toolbox
{
  public static final String NORTH = "North";
  public static final String SOUTH = "South";
  public static final String lineSeparator = System.getProperty("line.separator");
  public static final String fileSeparator = System.getProperty("file.separator");

  private Toolbox()
  {
    // utility class
  }

  public static Point centerScreen(final Window window)
  {
    final Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
    return new Point((int) (size.getWidth() - (double) window.getWidth()) >> 1,
                     (int) (size.getHeight() - (double) window.getHeight()) >> 1);
  }

  public static void openUrl(final String url)
  {
    final String os = System.getProperty("os.name");
    final Runtime runtime = Runtime.getRuntime();
    try
    {
      if (os.startsWith("Windows"))
      {
        String cmd = "rundll32 url.dll,FileProtocolHandler " + url;
        runtime.exec(cmd);
      }
      else if (os.startsWith("Mac OS"))
      {
        final Class fileMgr = Class.forName("com.apple.eio.FileManager");
        final Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[]{String.class});
        openURL.invoke(null, url);
      }
      else
      {
        final String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
        for (final String browser : browsers)
          if (runtime.exec(new String[]{"which", browser}).waitFor() == 0)
            if (browser == null)
              throw new Exception("Could not find web browser");
            else
              runtime.exec(new String[]{browser, url});
      }
    }
    catch (Exception x)
    {
      System.err.println("Exception occurred while invoking Browser!");
      x.printStackTrace();
    }
  }
}
